﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RonYee.Models;
using System.Data.Linq.SqlClient;

namespace RonYee.Controllers
{
    public class StoreController : Controller
    {
        RongeDb db = new RongeDb();
        //
        // GET: /Store/

        public ActionResult Index()
        {
            RonYee.Models.MvcPage Model = new MvcPage();
            Model.CurrentPageIndex = 1;  //当前页 默认第一页

            int SkinCount = (Model.CurrentPageIndex - 1) * Model.PageSize;

            //测试用例。实际用自己的逻辑获取数据
            List<Book> datesource = GetDataSource();//要分页的数据源
            Model.BookList = datesource.Where(r => r.BookId >= 0).Skip(SkinCount).Take(Model.PageSize).ToList();
            //测试用例。实际用自己的逻辑获取数据

            //是否开启Ajax分页
            Model.IsAjax = true;
            Model.TotalCount = datesource.Count;//获取记录总数

            //获取分页数据
            Model.BookList = datesource.Where(r => r.BookId >= 0).Skip(SkinCount).Take(Model.PageSize).ToList();
            return View(Model);

        }

        private List<Book> GetDataSource()
        {
            var book = from b in db.Books
                       where b.BookCount > 0
                       orderby b.BookName ascending
                       select b;
            List<Book> model = new List<Book>();
            foreach (var item in book) 
            {
                model.Add(item);
            }
            return model;
        }

        public ActionResult PageIndex(RonYee.Models.MvcPage Model)
        {

            //分页数据源。自己写逻辑获取。


            int SkinCount = (Model.CurrentPageIndex - 1) * Model.PageSize;

            //测试用例。实际用自己的逻辑获取数据
            List<Book> datesource = GetDataSource();//要分页的数据源
            Model.BookList = datesource.Where(r => r.BookId >= 0).Skip(SkinCount).Take(Model.PageSize).ToList();
            //测试用例。实际用自己的逻辑获取数据

            Model.TotalCount = datesource.Count;//获取记录总数
            if (Request.IsAjaxRequest())  //是AJAX请求的话返回AJAX分页数据
            {
                Model.IsAjax = true;
                return View(Model);
            }
            else
            {
                Model.IsAjax = false;
                return this.PartialView("Index", Model);
            }
            //return View(Model);
        }




        #region  //在（Books表）中对图书检索查阅

        [HttpPost]
        public ActionResult Search() 
        {
            string key = Request.Form["s"];
            if (key != null)
            {
                var search = from b in db.Books
                             where b.BookName.Contains(key) && b.BookCount > 0
                             orderby b.BookName ascending
                             select b;
                int count = (from b in db.Books
                             where b.BookName.Contains(key) && b.BookCount > 0
                             orderby b.BookName ascending
                             select b).Count();
                ViewBag.Count = count;

                return View(search);
            }
            else
            {
                var search = from b in db.Books
                             where b.BookCount > 0
                             orderby b.BookName ascending
                             select b;

                int count = (from b in db.Books
                             where b.BookCount > 0
                             orderby b.BookName ascending
                             select b).Count();
                ViewBag.Count = count;

                return View(search);
            }
        }

        public ActionResult Search(string key)
        {
            if (key != null)
            {
                var search = from b in db.Books
                             where b.BookType1==key && b.BookCount > 0
                             orderby b.BookName ascending
                             select b;

                return View(search);
            }
            else
            {
                return RedirectToAction("Index");
            }
        }
        //
        // GET: /Store/Details/5
        public ActionResult Details(int id) 
        {
            var book = from b in db.Books
                       where b.BookId == id
                       select b;

            Book b1 = (from b in db.Books
                       where b.BookId == id
                       select b).FirstOrDefault();
            var SameBooks = (from b in db.Books
                             where b.BookType1 == b1.BookType1
                             orderby b.BookCount descending
                             select b).Take(6);

            ViewData["same"] = SameBooks;

            return View(book);
        }

        #endregion

    }
}
